package bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author: admin
 * @Description:
 * @Date: 2019/3/7 10:57
 */

public class BioServer {

    private ServerSocket serverSocket=null;
    private int port;

    public BioServer(int port){
        this.port=port;
    }

    public void start(){
        try {
            serverSocket=new ServerSocket();
            serverSocket.bind(new InetSocketAddress(port));
            // 接收新连接线程
            new Thread(()->{
                while (true){
                    try {
                        Socket socket=serverSocket.accept();
                        new Thread(()->{
                            byte[] data=new byte[1024];
                            try {
                                InputStream inputStream=socket.getInputStream();
                                while (true){
                                    int len;
                                    while ((len=inputStream.read(data))!=-1){
                                        System.out.println(new String(data,0,len));
                                    }
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }).start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new BioServer(9999).start();
    }


}